/*
 * @(#)XfsimProcess.java        1.0 2000/05/09
 *
 * This file is part of Xfuzzy 3.0, a design environment for fuzzy logic
 * based systems.
 *
 * (c) 2000 IMSE-CNM. The authors may be contacted by the email address:
 *                    xfuzzy-team@imse.cnm.es
 *
 * Xfuzzy is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * Xfuzzy is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

package xfuzzy.xfsim.view;

import xfuzzy.xfsim.model.XfsimPipe;
import xfuzzy.xfsim.model.XfsimStatus;
import xfuzzy.xfsim.model.XfsimThread;

/**
 * Hilo de control del proceso de simulaci�n
 * 
 * @author Francisco Jos� Moreno Velo
 *
 */
public class XfsimProcess extends Thread {

	//----------------------------------------------------------------------------//
	//                            MIEMBROS P�BLICOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Tuber�a de comunicaci�n entre el proceso y la interfaz gr�fica
	 */
	private XfsimPipe pipe;
	
	/**
	 * Hilo de ejecuci�n del proceso de simulaci�n
	 */
	private Thread thread;
	
	/**
	 * Clase que se ejecuta en el hilo
	 */
	private XfsimThread runnable;
	
	/**
	 * Interfaz gr�fica
	 */
	private Xfsim xfsim;

	//----------------------------------------------------------------------------//
	//                                CONSTRUCTOR                                 //
	//----------------------------------------------------------------------------//

	/**
	 * Constructor
	 */
	public XfsimProcess(Xfsim xfsim) {
		super("xfsim thread 1");
		this.xfsim = xfsim;
		this.pipe = new XfsimPipe();
		this.runnable = new XfsimThread(xfsim.getSpecification(), xfsim.getConfiguration(),xfsim, pipe);
		thread = new Thread(runnable,"xfsim thread 2");
		thread.start();
		start();
	}

	//----------------------------------------------------------------------------//
	//                             M�TODOS P�BLICOS                               //
	//----------------------------------------------------------------------------//

	/**
	 * Detener la ejecuci�n
	 */
	public void sendStop() {
		this.pipe.putCommand(XfsimPipe.STOP);
	}

	/**
	 * Continuar la ejecuci�n
	 */
	public void sendContinue() {
		this.pipe.putCommand(XfsimPipe.CONTINUE);
	}

	/**
	 * Finalizar la ejecuci�n
	 */
	public void sendFinish() {
		this.pipe.putCommand(XfsimPipe.FINISH);
	}

	/**
	 * Ejecucion del proceso de simulaci�n
	 */
	public void run() {
		XfsimStatus status = this.pipe.get();
		xfsim.setStatus(status);
		while(status.getStatus() != XfsimStatus.FINISHED) {
			status = this.pipe.get();
			xfsim.setStatus(status);
		}
		try { thread.join(); } catch(InterruptedException e) {}
	}
}
